//定义路由转发器
import {createRouter, createWebHistory} from "vue-router"
import {getJwt} from "@/api/jwt.js";


//定义路由， 所谓的路由就是一个地址路由到一个组件上
const routes = [  // 修改1: 将变量名从 router 改为 routes
    {
        name: "main",
        //访问/main的时候就路由到下面这个组件
        path: "/main",
        component: () => import("@/components/view/Main.vue"),
        children: [
            {
                name: "customer",
                path: "customer",
                component: () => import("@/components/view/Customer.vue")
            },
            {
                name: "course",
                path: "course",
                component: () => import("@/components/view/Course.vue")
            }, {
                name: "coach",
                path: "coach",
                component: () => import("@/components/view/Coach.vue")
            }, {
                name: "pool",
                path: "pool",
                component: () => import("@/components/view/Pool.vue")
            },{
                name: "calendar",
                path: "calendar",
                component: () => import("@/components/view/Calendar.vue")
            },{
                name: "dashboard",
                path: "dashboard",
                component: () => import("@/components/view/Dashboard.vue")
            },{
                name: "admin",
                path: "admin",
                component: () => import("@/components/view/Admin.vue")
            },{
                name: "personalTrain",
                path: "personalTrain",
                component: () => import("@/components/view/PersonalTrain.vue")
            }, // 添加member路由
            {
                name: "member",
                path: "member",
                component: () => import("@/components/view/member.vue") // 可以根据需要指定具体组件
            },{
                name: "teach",
                path: "teach",
                component: () => import("@/components/view/Teach.vue")
            }
        ]
    }, {
        name: "index",
        path: "/",
        redirect: "/main"

    }, {
        name: "login",
        path: "/login",
        component: () => import("@/components/view/Login.vue")
    }
];

//定义路由转发器
const router = createRouter({
    routes,  // 修改2: 使用正确的属性名 routes
    history: createWebHistory()
})

//配置路由守卫：拦截所有路由，判断用户是否登录
router.beforeEach((to, from, next) => {
    let jwt = getJwt();
    if (jwt) {
        if (to.name === "login") {
            next("/main")
        } else {
            next();
        }
    } else {
        if (to.name !== "login") {
            next("/login");
        } else {
            next();
        }
    }
})
//导出
export default router;
